<?php 

	include('cms.php');
	
	if(!isset($_POST)){
		die("No post data");
	}
	
	$insert = empty($_POST['idcontent']);
	
	if($insert){
		$c = new Content();
	}else{
		$c = DataLatte::oneof('Content', "SELECT * FROM cmsContent WHERE idcontent = '" . $_POST['idcontent'] . "'");
	}
	
	//Some error?
	if(!$c) die("Content not found");
	
	//Acquire info from POST
	DataLatte::recognize($_POST, $c);
	
	//Set modification date
	$c->modified = DataLatte::getsingle("SELECT NOW()");
	
	//Clean tags from title
	$c->title = strip_tags($c->title);
	
	//Trim title, just in case
	if(strlen($c->title) > 100)
		$c->title = substr($c->title, 0, 99);
	
	if($insert){
		$c->created = DataLatte::getsingle("SELECT NOW()");
		$c->cmsUser_iduser = Session::user();
		$c->insert();
		Log::add(CREATE, $c->idcontent);
	}else{
		$c->update();
		Log::add(MODIFY, $c->idcontent);
	}
	
	echo $c->idcontent;

?>